﻿function Socket(ip, port, msgCallBack, closeCallBack) {

    var conn = new WebSocket("ws://" + ip + ":" + port);

    conn.onerror = function (error) {
        console.log('WebSocket Error ' + error);
    };

    conn.onmessage = function (msg) {
        msgCallBack(msg.data);
    };

    conn.onclose = function () {
        closeCallBack();
    }

    this.Send = function (tid, content) {

        var obj = { 'tid': tid || 0, 'content': content };
        var msg = JSON.stringify(obj);

        conn.send(msg);
    }

    this.Close = function () {
        conn.close();
    }
}